<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>闭包</title>
</head>
<body>
    不做防抖<input type="text" name="" id="root1">

    </br></br></br></br></br>
    
    做防抖<input type="text" name="" id="root2">


<script>
    const text1 = document.getElementById('root1')
    const text2 = document.getElementById('root2')
    // 不做防抖
    text1.addEventListener('input',function(e){
        console.log(e.target.value);
    })
    
    // 做防抖
    function debounce(fn,delay){
        let time = null
        return function(){
            let context = this //window
            args = arguments
            if(time){
                clearTimeout(time)  // 取消之前的settimeout
            }
            time = setTimeout(function(){
                fn.apply(context,args)
            },delay)
        }
    }
    function ajax(content){
        console.log(content);
    }
    // 闭包
    const textFn = debounce(ajax,500)
    text2.addEventListener('input',function(e){
        textFn(e.target.value);
    })
</script>
</body>
</html>